#!/usr/bin/python

import os
import string
import jsonss
from jsonss.error import JSONSSError

print "Content-type: text/plain; charset=utf-8\r\n\r\n",

def onMyEvent (d):
    print "Got MyEvent with attached data: " + d

try:
    url = "http://" + os.environ["SERVER_NAME"] + os.environ["SCRIPT_NAME"]
    prx = jsonss.proxyFromUrl(string.replace(url, "/cli.py", "/srv.py"))

    prx.MyService.onMyEvent = onMyEvent
    print "Got proxy object, MyService.serviceUrl is: " + prx.MyService.serviceUrl

    print "MyService.MyMethod(True): " + prx.MyService.MyMethod(True)
    print "MyService.MyMethod(\"a string\"): " + prx.MyService.MyMethod("a string")
    print "MyService.MyMethod(42): " + prx.MyService.MyMethod(42)

    print "MyService.MyProperty: " + prx.MyService.MyProperty
except JSONSSError as e:
    print ":( Exception: " + e.getMessage()

try:
    print "Trying to set MyService.MyProperty:"
    prx.MyService.MyProperty = "override"
    print "Hmmm... Succeed :("
except JSONSSError as e:
    print "Fine. Exception: " + e.getMessage()
